//
//  FeedbackViewController.m
//  TTAddressBook
//
//  Created by WangJuan on 12-1-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>

#import "FeedbackViewController.h"
#import "SettingConstants.h"
#import "CommTools.h"
#define text_height 133
@implementation FeedbackViewController
@synthesize numFieldBgImage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) 
    {
        self.title = @"意见建议";
    }
    
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //返回按钮
    UIBarButtonItem *goBackButton = [[UIBarButtonItem alloc]
                                     initWithTitle:@"返回"
                                     style:UIBarButtonItemStylePlain
                                     target:self
                                     action:@selector(goBackClick)];
    self.navigationItem.leftBarButtonItem = goBackButton;
    [goBackButton release];
    
    UIBarButtonItem *sendButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"发送"
                                   style:UIBarButtonItemStylePlain
                                   target:self
                                   action:@selector(sendClick)];
    self.navigationItem.rightBarButtonItem = sendButton;
    [sendButton release];
    
//    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 5.0)
//    {
//        [goBackButton setBackgroundImage:[UIImage imageNamed: kNavigationBarButtonPng] forState:(UIControlStateNormal) barMetrics:0];
//        [goBackButton setBackgroundImage:[UIImage imageNamed: kNavigationBarButtonOnPng] forState:(UIControlStateSelected) barMetrics:0];
//        
//        [sendButton setBackgroundImage:[UIImage imageNamed: kNavigationBarButtonPng] forState:(UIControlStateNormal) barMetrics:0];
//        [sendButton setBackgroundImage:[UIImage imageNamed: kNavigationBarButtonOnPng] forState:(UIControlStateSelected) barMetrics:0];
//    }
//    
    UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    backgroundView.image = [UIImage imageNamed:kSettingBackGroundPng];
    [self.view addSubview:backgroundView];
    [self.view sendSubviewToBack:backgroundView];
    [backgroundView release];
        
    containerView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 260, text_height)];
    opinionTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 260, text_height)];
//    opinionTextView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(0, 0, 250, 60)];
//    opinionTextView.minNumberOfLines = 2;
//    opinionTextView.maxNumberOfLines = 2;
//    opinionTextView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
    opinionTextView.backgroundColor = [UIColor clearColor];
    opinionTextView.returnKeyType = UIReturnKeyDefault;
    opinionTextView.font = [UIFont systemFontOfSize:15.0f];
    [opinionTextView.layer setCornerRadius:10];
    opinionTextView.backgroundColor = [UIColor whiteColor];
    opinionTextView.delegate = self;
//    opinionTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//    opinionTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
    
    UIImage *rawEntryBackground = [UIImage imageNamed:kFeedBackInputBgPng];
    UIImage *entryBackground = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    entryImageView = [[[UIImageView alloc] initWithImage:entryBackground] autorelease];
    entryImageView.frame = CGRectMake(0, 0, 260, text_height);
    entryImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    [containerView addSubview:opinionTextView];
    [containerView addSubview:entryImageView];
    [self.view addSubview:containerView];
    
     [opinionTextView becomeFirstResponder];
    
    
}

- (void)viewDidUnload
{
    [self setNumFieldBgImage:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)backgroundClick:(id)sender 
{
    [opinionTextView resignFirstResponder];
}

- (void)goBackClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sendClick
{
    if ( [opinionTextView.text isEqualToString:@""] )
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"意见不能为空" message:@"请输入您对保合宝的建议或意见，谢谢！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alertView show];
        [alertView release];
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"谢谢您的意见！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alertView show];
        [alertView release];
        opinionTextView.text = @"";   
    }
}

- (void)dealloc 
{
    [containerView release];
    [opinionTextView release];
    [numFieldBgImage release];
    [entryImageView release];
    [super dealloc];
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    return  YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{

}

#pragma mark UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    UIImage *rawEntryBackground = [UIImage imageNamed:kFeedBackInputOnBgPng];
    entryImageView.image = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    
    return YES; 
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    UIImage *rawEntryBackground = [UIImage imageNamed:kFeedBackInputBgPng];
    entryImageView.image = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
}

@end
